package com.yycx.module.user.provider.service.impl;

import cn.hutool.core.map.MapUtil;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.RedisUtils;
import com.yycx.module.user.client.entity.AppUserPrivatedata;
import com.yycx.module.user.provider.mapper.AppUserPrivatedataMapper;
import com.yycx.module.user.provider.service.AppUserPrivatedataService;
import com.yycx.module.user.provider.service.AppUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 用户身份认证资料（开卡完善信息表,新增常用旅客信息表）接口实现类
 *
 * @author flyme
 * @date 2022-03-01
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AppUserPrivatedataServiceImpl extends BaseServiceImpl<AppUserPrivatedataMapper, AppUserPrivatedata> implements AppUserPrivatedataService {
    @Autowired
    private RedisUtils redisUtils;


    @Autowired
    private AppUserService appUserService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, AppUserPrivatedata aup, EntityMap extra) {
        Long userId= OpenHelper.getUserId();
        aup.setUserId(userId);
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<AppUserPrivatedata> cq, AppUserPrivatedata aup, EntityMap requestMap) {
        cq.eq(AppUserPrivatedata.class, "userId");
        cq.orderByDesc("privateData.createTime");
        return ResultBody.ok();
    }




    @Override
    public AppUserPrivatedata getByUserId(Long userId) {
        CriteriaQuery cq = new CriteriaQuery(AppUserPrivatedata.class);
        cq.eq(true,"userId",userId);
        return getOne(cq);
    }

    @Override
    public ResultBody realNameAuthSub(Map<String, Object> params) {
        Long userId= MapUtil.getLong(params,"userId");
        Integer isPrivatedata= MapUtil.getInt(params,"isPrivatedata");
        CriteriaUpdate cu = new CriteriaUpdate();
        cu.set(true,"isPrivatedata",isPrivatedata);
        cu.eq(true,"userId",userId);
        appUserService.update(cu);
        return  ResultBody.msg("操作成功");
    }

    public   void insertAppUserPrivatedata(Map params) {
        Long userId=OpenHelper.getUserId();
        AppUserPrivatedata aup = getByUserId(userId);
        String userName=MapUtil.getStr(params,"userName");
        String cardNo=MapUtil.getStr(params,"cardNo");
        ApiAssert.isNotEmpty("姓名不允许为空",userName);
        ApiAssert.isNotEmpty("身份证号不允许为空", cardNo);
        if(FlymeUtils.isEmpty(aup)){
            aup=new AppUserPrivatedata();
        }
        aup.setUserId(userId);
        aup.setUserName(userName);
        aup.setIdentityNo(cardNo);
        saveOrUpdate(aup);
        CriteriaUpdate cu = new CriteriaUpdate();
        cu.set(true,"privateDataId",aup.getPrivateDataId());
        cu.set(true,"isPrivatedata",2);
        cu.set(true,"authState",2);
        cu.eq(true,"userId",userId);
        appUserService.update(cu);
    }
}
